package com.huawei.appgallery.forum.option.post.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.common.media.api.IMediaSelectProtocol;
import com.huawei.appgallery.common.media.api.IMediaSelectResult;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import com.huawei.appgallery.forum.option.api.IPublishPostActivityProtocol;
import com.huawei.appgallery.forum.option.api.IPublishPostActivityResult;
import com.huawei.appgallery.forum.option.post.bean.PublishPostRes;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.hmf.annotation.ActivityDefine;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import java.io.File;
import java.util.List;
import java.util.regex.Pattern;
import o.azb;
import o.bqx;
import o.bqy;
import o.brc;
import o.brd;
import o.bse;
import o.bxz;
import o.bya;
import o.byc.c;
import o.byc.d;
import o.byc.e;
import o.byc.f;
import o.byc.j;
import o.byd;
import o.byl;
import o.bym;
import o.byo;
import o.byq;
import o.byr;
import o.bys;
import o.byu;
import o.byz;
import o.cgv;
import o.dnw;
import o.doo;
import o.epv;
import o.CollectCheck;
import o.evk;
import o.evx;
import o.fho;
import o.fiu;
import o.fiv;
import o.fjb;
import o.fjc;
import o.fjf;
import o.fjo;
import o.fjq;

@ActivityDefine(alias = "option.publish", protocol = IPublishPostActivityProtocol.class, result = IPublishPostActivityResult.class)
public class PublishPostActivity extends AbstractBaseActivity implements byu, byq, byr, bys {
    private boolean ʻॱ = true;
    private byd ʽॱ;
    private TextView ʾ;
    private boolean ʿ = false;
    private EditText ˈ;
    private InsertImgToolbar ˉ;
    private TextView ˊˊ;
    private RichTextEditor ˊˋ;
    private int ˊॱ;
    private TextView ˊᐝ;
    private ScrollView ˋˊ;
    private boolean ˋˋ = false;
    private fiv ˋॱ = fiv.ॱ(this);
    private int ˋᐝ;
    private boolean ˌ = false;
    private Handler ˍ;
    private InputFilter ˎˎ = new bym(60, this);
    private ProgressBar ˏॱ;
    private ViewGroup ͺ;
    private ImageView ॱˊ;
    private boolean ॱˋ;
    private boolean ॱˎ;
    private String ᐝॱ;

    public void onCreate(@Nullable Bundle bundle) {
        bqx.ˊ("PublishPostActivity", "onCreate");
        super.onCreate(bundle);
        setContentView(f.ᐝ);
        if (bundle != null) {
            this.ॱˎ = bundle.getBoolean("key_isupdate");
            this.ᐝॱ = bundle.getString("key_domainid");
            try {
                if (bundle.getSerializable("key_publish_data") instanceof byd) {
                    this.ʽॱ = (byd) bundle.getSerializable("key_publish_data");
                }
            } catch (Exception e) {
                bqx.ॱ("PublishPostActivity", "get publish data from saveinstance failed");
            }
        } else {
            IPublishPostActivityProtocol iPublishPostActivityProtocol = (IPublishPostActivityProtocol) this.ˋॱ.ॱ();
            this.ॱˎ = iPublishPostActivityProtocol.getIsUpdate();
            this.ᐝॱ = iPublishPostActivityProtocol.getDomainId();
            this.ʽॱ = iPublishPostActivityProtocol.getPublishData();
        }
        if (this.ʽॱ == null) {
            bqx.ॱ("PublishPostActivity", "publishPostData == null");
            this.ˋᐝ = 1;
            finish();
            return;
        }
        ˎˎ();
        ॱˊ();
        ͺ();
        if (!this.ॱˎ) {
            byd ॱ = ॱ(this.ʽॱ.ˊ());
            if (ॱ != null) {
                bqx.ˏ("PublishPostActivity", "have useful draft");
                this.ʽॱ = ॱ;
            }
        }
        ˊ(this.ʽॱ);
    }

    private void ॱˊ() {
        cgv.ˋ(this, e.ॱ, e.ˎ);
        View findViewById = findViewById(d.ʻॱ);
        azb.ˏ(findViewById);
        ((ViewGroup) findViewById.findViewById(d.ᐝ)).setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PublishPostActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void onClick(View view) {
                this.ˏ.ˋᐝ = 3;
                this.ˏ.finish();
            }
        });
        ((TextView) findViewById.findViewById(d.ˋˊ)).setText(this.ॱˎ ? j.ʻॱ : j.ॱˊ);
        this.ͺ = (ViewGroup) findViewById.findViewById(d.ˋॱ);
        this.ͺ.setContentDescription(getResources().getString(this.ॱˎ ? j.ʻॱ : j.ॱˊ));
        this.ͺ.setOnClickListener(new evx(this) {
            final /* synthetic */ PublishPostActivity ˋ;

            {
                this.ˋ = r1;
            }

            public void ˋ(View view) {
                this.ˋ.ˊᐝ();
            }
        });
        this.ॱˊ = (ImageView) this.ͺ.findViewById(d.ͺ);
        this.ॱˊ.setImageResource(c.ˊ);
        this.ˏॱ = (ProgressBar) findViewById.findViewById(d.ʾ);
    }

    private void ͺ() {
        this.ʾ = (TextView) findViewById(d.ʼॱ);
        this.ˊᐝ = (TextView) findViewById(d.ʼ);
        this.ˈ = (EditText) findViewById(d.ʿ);
        this.ˊˋ = (RichTextEditor) findViewById(d.ʻ);
        this.ˊˋ.setOnImageNumberListener(this);
        this.ˊˋ.setOnCharNumberListener(this);
        this.ˊˋ.setOnContentIsEmptyListener(this);
        this.ˊˊ = (TextView) findViewById(d.ˏॱ);
        this.ˉ = (InsertImgToolbar) findViewById(d.ॱˊ);
        this.ˋˊ = (ScrollView) findViewById(d.ॱˎ);
        azb.ˋ(this.ˋˊ);
        int ˊ = epv.ˊ(this, 1);
        LayoutParams layoutParams = (LayoutParams) this.ˉ.getLayoutParams();
        layoutParams.gravity = 17;
        layoutParams.width = ˊ;
        this.ˉ.setLayoutParams(layoutParams);
        this.ˉ.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PublishPostActivity ˋ;

            {
                this.ˋ = r1;
            }

            public void onClick(View view) {
                this.ˋ.ᐝॱ();
            }
        });
        this.ˈ.addTextChangedListener(new TextWatcher(this) {
            final /* synthetic */ PublishPostActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                this.ˏ.ˊॱ = editable.length();
                this.ˏ.ˌ = ((double) this.ˏ.ˊॱ) >= 54.0d;
                if (this.ˏ.ˊॱ < 60) {
                    this.ˏ.ˋˋ = false;
                }
                this.ˏ.ˎ(true);
                this.ˏ.ˊˋ();
            }
        });
        this.ˈ.setOnFocusChangeListener(new OnFocusChangeListener(this) {
            final /* synthetic */ PublishPostActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void onFocusChange(View view, boolean z) {
                boolean z2;
                this.ॱ.ˎ(z);
                InsertImgToolbar ॱ = this.ॱ.ˉ;
                if (z) {
                    z2 = false;
                } else {
                    z2 = true;
                }
                ॱ.setDisable(z2);
                if (!z) {
                    this.ॱ.ˋˋ = false;
                }
            }
        });
        this.ˈ.setFilters(new InputFilter[]{this.ˎˎ});
        ͺॱ();
        findViewById(d.ˈ).setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PublishPostActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void onClick(View view) {
                this.ॱ.ˊˋ.ˊ();
            }
        });
    }

    private void ˎ(boolean z) {
        if (z) {
            if (this.ˌ) {
                this.ʾ.setText(getResources().getString(j.ͺ, new Object[]{Integer.valueOf(this.ˊॱ), Integer.valueOf(60)}));
                this.ʾ.setVisibility(0);
            } else {
                this.ʾ.setVisibility(8);
            }
            if (this.ˋˋ) {
                this.ʾ.setTextColor(getResources().getColor(e.ʼ));
                AlphaAnimation alphaAnimation = new AlphaAnimation(BarrierParameters.RESTITUTION, 1.0f);
                alphaAnimation.setDuration(10);
                alphaAnimation.setRepeatCount(7);
                alphaAnimation.setRepeatMode(2);
            } else {
                this.ʾ.setTextColor(getResources().getColor(e.ˊ));
            }
            this.ˋˊ.scrollTo(0, 0);
            return;
        }
        this.ʾ.setVisibility(8);
    }

    private byd ॱ(int i) {
        return ((bxz) fho.ˏ().ˋ("Option").ॱ(bxz.class)).ˏ(i);
    }

    private void ˊ(@NonNull byd o_byd) {
        this.ˈ.setText(o_byd.ॱ());
        this.ˊˋ.ˏ(o_byd.ˋ());
        Message obtain = Message.obtain();
        obtain.what = 3;
        this.ˍ.sendMessageDelayed(obtain, 200);
    }

    private void ᐝॱ() {
        String[] strArr = new String[]{"image/jpeg", "image/jpg", "image/png", "image/webp", "image/gif", "image/bmp"};
        fjb ˎ = fho.ˏ().ˋ("Media").ˎ("MediaSelect");
        IMediaSelectProtocol iMediaSelectProtocol = (IMediaSelectProtocol) ˎ.ˊ();
        iMediaSelectProtocol.setMediaType("image");
        iMediaSelectProtocol.setMimeTyes(strArr);
        iMediaSelectProtocol.setMaxSelectSize(9);
        iMediaSelectProtocol.setMaxSelectFileSize(10485760);
        fiu.ˊ().ॱ((Context) this, ˎ, new fjc<IMediaSelectResult>(this) {
            final /* synthetic */ PublishPostActivity ˋ;

            {
                this.ˋ = r1;
            }

            public /* synthetic */ void ˎ(int i, Object obj) {
                ˋ(i, (IMediaSelectResult) obj);
            }

            public void ˋ(int i, IMediaSelectResult iMediaSelectResult) {
                if (ˊ() instanceof PublishPostActivity) {
                    ((PublishPostActivity) ˊ()).ˏ(i, iMediaSelectResult);
                }
            }
        });
    }

    private void ˏ(int i, IMediaSelectResult iMediaSelectResult) {
        if (i == -1 && iMediaSelectResult != null) {
            try {
                List<OriginalMediaBean> selectedMedias = iMediaSelectResult.getSelectedMedias();
                if (!CollectCheck.isEmpty((List) selectedMedias)) {
                    for (OriginalMediaBean originalMediaBean : selectedMedias) {
                        if (originalMediaBean == null) {
                            bqx.ॱ("PublishPostActivity", "originalMediaBean == null or originalMediaBean.getData is empty");
                        } else if (TextUtils.isEmpty(originalMediaBean.ʽ())) {
                            bqx.ॱ("PublishPostActivity", "originalMediaBean.getData is empty" + originalMediaBean.ᐝ());
                        } else {
                            byl o_byl = new byl(originalMediaBean);
                            o_byl.ˏ(this.ॱˎ);
                            ˎ(o_byl);
                            this.ˊˋ.ˋ(o_byl);
                        }
                    }
                }
            } catch (Exception e) {
                bqx.ॱ("PublishPostActivity", "cast error");
            }
        }
    }

    private void ʼॱ() {
        if (this.ॱˋ) {
            bqx.ˏ("PublishPostActivity", "post is empty,no need to save post");
            return;
        }
        ˋˊ();
        ((bxz) fho.ˏ().ˋ("Option").ॱ(bxz.class)).ˏ(this.ʽॱ);
    }

    private void ˋˊ() {
        if (this.ʽॱ != null) {
            if (this.ˈ != null) {
                this.ʽॱ.ˏ(Pattern.compile("\\s+|\t|\r|\n").matcher(this.ˈ.getText().toString()).replaceAll(HwAccountConstants.BLANK).trim());
            }
            if (this.ˊˋ != null) {
                this.ʽॱ.ˎ(this.ˊˋ.ˏ());
                this.ʽॱ.ˎ(this.ˊˋ.ˊ(true));
                this.ʽॱ.ˊ(this.ˊˋ.ॱ());
            }
        }
    }

    private void ˊˊ() {
        bxz o_bxz = (bxz) fho.ˏ().ˋ("Option").ॱ(bxz.class);
        if (o_bxz.ˏ(this.ʽॱ.ˊ()) != null) {
            o_bxz.ˊ();
        }
    }

    public void finish() {
        if (this.ʽॱ == null) {
            super.finish();
            return;
        }
        if (this.ˋᐝ != -1) {
            if (!this.ॱˎ) {
                ʼॱ();
            }
            setResult(this.ˋᐝ);
        } else {
            if (!this.ॱˎ) {
                ˊˊ();
            }
            fjf ˊ = fjf.ˊ(this);
            ((IPublishPostActivityResult) ˊ.ॱ()).setPublishPostResult(this.ʽॱ);
            setResult(this.ˋᐝ, ˊ.ˊ());
        }
        if (this.ॱˎ) {
            dnw.ˊ(new File(byz.ˎ(true, String.valueOf(this.ʽॱ.ˊ()))));
        }
        super.finish();
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
    }

    private void ˎ(byl o_byl) {
        ((bya) fho.ˏ().ˋ("Option").ॱ(bya.class)).ॱ(this.ᐝॱ, o_byl, this.ʽॱ.ˊ()).ˏ(new fjq<byl>(this) {
            final /* synthetic */ PublishPostActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void onComplete(fjo<byl> o_fjo_o_byl) {
                if (this.ॱ.ʿ) {
                    if (o_fjo_o_byl.ˊ()) {
                        byl o_byl = (byl) o_fjo_o_byl.ॱ();
                        if (o_byl != null && o_byl.ॱॱ() == 2) {
                            this.ॱ.ˋˋ();
                            return;
                        }
                    }
                    bqx.ˊ("PublishPostActivity", "upload image failed");
                    this.ॱ.ˏ(false);
                    evk.ˏ(this.ॱ.getString(j.ᐝ));
                }
            }
        });
    }

    public void aa_() {
        this.ˊˊ.setVisibility(0);
        this.ˉ.setDisable(false);
    }

    public void ᐝ() {
        this.ˊˊ.setVisibility(8);
        this.ˉ.setDisable(true);
    }

    public void ˏ(int i) {
        this.ˊᐝ.setText(getResources().getString(j.ͺ, new Object[]{Integer.valueOf(i), Integer.valueOf(10000)}));
        this.ˊᐝ.setVisibility(0);
    }

    public void ˎ() {
        this.ˊᐝ.setVisibility(8);
    }

    public void ˋ() {
        this.ʻॱ = false;
        ˊˋ();
    }

    public void ˏ() {
        this.ʻॱ = true;
        ˊˋ();
    }

    private void ˊˋ() {
        boolean z = true;
        if (this.ʻॱ || ˉ()) {
            this.ͺ.setClickable(false);
            this.ॱˊ.setImageResource(c.ˊ);
        } else {
            this.ͺ.setClickable(true);
            this.ॱˊ.setImageResource(c.ॱ);
        }
        if (!(this.ʻॱ && ˉ())) {
            z = false;
        }
        this.ॱˋ = z;
    }

    private void ˊ(boolean z) {
        boolean z2;
        int i = 8;
        boolean z3 = true;
        this.ʿ = z;
        this.ˏॱ.setVisibility(z ? 0 : 8);
        ViewGroup viewGroup = this.ͺ;
        if (!z) {
            i = 0;
        }
        viewGroup.setVisibility(i);
        EditText editText = this.ˈ;
        if (z) {
            z2 = false;
        } else {
            z2 = true;
        }
        editText.setEnabled(z2);
        RichTextEditor richTextEditor = this.ˊˋ;
        if (z) {
            z2 = false;
        } else {
            z2 = true;
        }
        richTextEditor.setEnable(z2);
        InsertImgToolbar insertImgToolbar = this.ˉ;
        if (z) {
            z3 = false;
        }
        insertImgToolbar.setDisable(z3);
    }

    private void ˊᐝ() {
        if (ˉ()) {
            bqx.ॱ("PublishPostActivity", "titleIsEmpty");
        } else if (doo.ˊ((Context) this)) {
            ˏ(true);
            ˋˋ();
        } else {
            Toast.makeText(this, getString(bse.ˏ.ˏ()), 0).show();
        }
    }

    private boolean ˉ() {
        if (this.ˈ != null) {
            return TextUtils.isEmpty(Pattern.compile("\\s*|\t|\r|\n").matcher(this.ˈ.getText().toString().trim()).replaceAll(""));
        }
        bqx.ॱ("PublishPostActivity", "postTitleEditText == null");
        return true;
    }

    private void ˋˋ() {
        if (ˌ()) {
            ˋˊ();
            ((brc) fho.ˏ().ˋ("Base").ॱ(brc.class)).ˋ(new b(this.ʽॱ, this.ॱˎ).ॱ(this.ᐝॱ).ॱ(), new brc.e<byo, PublishPostRes>(this) {
                final /* synthetic */ PublishPostActivity ॱ;

                {
                    this.ॱ = r1;
                }

                public /* synthetic */ void ˏ(Object obj, Object obj2) {
                    ॱ((byo) obj, (PublishPostRes) obj2);
                }

                public void ॱ(byo o_byo, PublishPostRes publishPostRes) {
                }

                public void ˋ(byo o_byo, PublishPostRes publishPostRes) {
                    this.ॱ.ˏ(false);
                    if (publishPostRes.getResponseCode() == 0 && publishPostRes.getRtnCode_() == 0) {
                        if (!this.ॱ.ॱˎ) {
                            this.ॱ.ʽॱ.ॱ(publishPostRes.ˊ());
                        }
                        byz.ˋ(true, String.valueOf(this.ॱ.ʽॱ.ˊ()));
                        this.ॱ.ˋᐝ = -1;
                        brd.ॱ.ॱ(bqy.ˋ().ˎ(this.ॱ), this.ॱ.ᐝॱ, String.valueOf(this.ॱ.ʽॱ.ˊ()));
                        this.ॱ.finish();
                        return;
                    }
                    if (publishPostRes.getRtnCode_() == 3004) {
                        if (this.ॱ.ॱˎ) {
                            evk.ˏ(this.ॱ.getString(bse.ˏ.ˏ("edit", 3004)));
                        } else {
                            evk.ˏ(this.ॱ.getString(bse.ˏ.ˏ("post", 3004)));
                        }
                    } else if (publishPostRes.getRtnCode_() == 400015) {
                        evk.ˏ(this.ॱ.getString(j.ˏॱ));
                    } else {
                        evk.ˏ(this.ॱ.getString(bse.ˏ.ॱ(publishPostRes.getRtnCode_()).ˏ()));
                    }
                    this.ॱ.ˋᐝ = 5;
                    bqx.ॱ("PublishPostActivity", "publish post failed");
                }
            });
        }
    }

    private boolean ˌ() {
        List<byl> ˎ = this.ˊˋ.ˎ();
        if (CollectCheck.isEmpty((List) ˎ)) {
            return true;
        }
        boolean z = true;
        for (byl o_byl : ˎ) {
            if (o_byl.ॱॱ() != 2) {
                z = false;
                if (o_byl.ॱॱ() == 3) {
                    bqx.ˊ("PublishPostActivity", "image upload failed, retry.");
                    ˎ(o_byl);
                }
            }
            z = z;
        }
        return z;
    }

    private void ˎˎ() {
        this.ˍ = new Handler(this, Looper.getMainLooper()) {
            final /* synthetic */ PublishPostActivity ˏ;

            public void handleMessage(Message message) {
                if (message.what == 0) {
                    this.ˏ.ˊ(false);
                } else if (message.what == 1) {
                    this.ˏ.ˊ(true);
                } else if (message.what == 2) {
                    this.ˏ.ˋᐝ();
                } else if (message.what == 3) {
                    this.ˏ.ˍ();
                } else {
                    bqx.ˊ("PublishPostActivity", "invalid msg");
                }
            }
        };
    }

    private void ˋᐝ() {
        if (this.ˈ != null) {
            this.ˈ.requestFocus();
            Object systemService = getSystemService("input_method");
            if (systemService instanceof InputMethodManager) {
                ((InputMethodManager) systemService).showSoftInput(this.ˈ, 0);
            }
        }
    }

    private void ˍ() {
        if (this.ˈ != null) {
            this.ˈ.requestFocus();
            this.ˈ.setSelection(0);
        }
    }

    private void ˏ(boolean z) {
        Message obtain = Message.obtain();
        obtain.what = z ? 1 : 0;
        if (this.ˍ != null) {
            this.ˍ.sendMessage(obtain);
        }
    }

    private void ͺॱ() {
        Message obtain = Message.obtain();
        obtain.what = 2;
        if (this.ˍ != null) {
            this.ˍ.sendMessageDelayed(obtain, 50);
        }
    }

    public void ˊ() {
        this.ˋˋ = true;
        ˎ(true);
    }

    public void onSaveInstanceState(Bundle bundle) {
        ˋˊ();
        bundle.putSerializable("key_publish_data", this.ʽॱ);
        bundle.putSerializable("key_isupdate", Boolean.valueOf(this.ॱˎ));
        bundle.putSerializable("key_domainid", this.ᐝॱ);
        super.onSaveInstanceState(bundle);
    }
}
